package org.springblade.upgrade.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.upgrade.log.entity.PackageLog;
import org.springblade.upgrade.log.mapper.PackageLogMapper;
import org.springblade.upgrade.log.model.PackageLogBean;
import org.springblade.upgrade.log.service.IPackageLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Version 1.0
 */
@Service
public class PackageLogServiceImpl extends BaseServiceImpl<PackageLogMapper, PackageLog> implements IPackageLogService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addData(PackageLogBean param) throws Exception {
        //检查 TODO
        //保存
        PackageLog entity = param;
        return this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateData(PackageLogBean param) throws Exception {
        //检查 TODO
        //更新
        PackageLog newEntity = param;
        return this.updateById(newEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteData(PackageLogBean param) throws Exception {
        if (param.getIdList() != null && param.getIdList().isEmpty()) {
            UpdateWrapper<PackageLog> del = new UpdateWrapper<>();
            del.lambda().in(PackageLog::getId, param.getIdList());
            return this.remove(del);
        }
        if (param.getId() != null) {
            return this.removeById(param.getId());
        }
        return true;
    }

    @Override
    public PackageLogBean findBySpec(PackageLogBean param) {
        List<PackageLogBean> list = this.baseMapper.queryList(param);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<PackageLogBean> findListBySpec(PackageLogBean param) {
        return this.baseMapper.queryList(param);
    }

    @Override
    public IPage<PackageLogBean> findPageFrontBySpec(IPage<PackageLogBean> page, PackageLogBean param) {
        param.setUploadType("front");
        List<PackageLogBean> dataList = baseMapper.queryPageList(page, param);
        return page.setRecords(dataList);
    }

    @Override
    public IPage<PackageLogBean> findPageBackendBySpec(IPage<PackageLogBean> page, PackageLogBean param) {
        param.setUploadType("backend");
        List<PackageLogBean> dataList = baseMapper.queryPageList(page, param);
        return page.setRecords(dataList);
    }
}

